//
//  ImagesXMLConvert.m
//  XMLTest
//
//  Created by Adiljan on 2019/2/18.
//  Copyright © 2019年 Adiljan. All rights reserved.
//

#import "ImagesXMLConvert.h"

@interface ImagesXMLConvert ()<NSXMLParserDelegate>
{
    //用于取xml节点的值
    NSString *value;
}
@end

@implementation ImagesXMLConvert

ImagesModel *_imagelist;
Picture_list *pl;


-(ImagesModel *)DeserializeXML:(NSString *)fromFile{
    _imagelist=[[ImagesModel alloc] init];
    _imagelist.data.picture_list = [NSMutableArray new];
    pl=[[Picture_list alloc] init];
    NSString *filePath =  [[NSBundle mainBundle] pathForResource:fromFile ofType:@"xml"];
    
    NSURL *url = [[NSURL alloc]initFileURLWithPath:filePath];
    NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url];
    
    [parser setShouldProcessNamespaces:NO];
    
    [parser setShouldReportNamespacePrefixes:NO];
    
    [parser setShouldResolveExternalEntities:NO];
    
    [parser setDelegate:self];
    
    [parser parse];
    
    return _imagelist;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
    NSString *str=[[NSString alloc] initWithString:value];
    
    if ([elementName isEqualToString:@"code"]) {
        _imagelist.status.code=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"msg"]) {
        _imagelist.status.msg=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"picture_num"]) {
        _imagelist.data.picture_num=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"picture_list"]) {
        [_imagelist.data.picture_list addObject:pl];
    }
    if ([elementName isEqualToString:@"picture_name"]) {
        pl.picture_name=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"picture_scope"]) {
        pl.picture_scope=str;
    }
    if ([elementName isEqualToString:@"picture_sign"]) {
        pl.picture_sign=str;
    }
    if ([elementName isEqualToString:@"is_cover"]) {
        pl.is_cover=str;
    }
    if ([elementName isEqualToString:@"width"]) {
        pl.width=str;
    }
    if ([elementName isEqualToString:@"height"]) {
        pl.height=str;
    }
    if ([elementName isEqualToString:@"img_src"]) {
        pl.img_src=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"m_width"]) {
        pl.m_width=str;
    }
    if ([elementName isEqualToString:@"m_height"]) {
        pl.m_height=str;
    }
    if ([elementName isEqualToString:@"pic_big_src"]) {
        pl.pic_big_src=str;
    }
    
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

{
    
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{
    value=string;
    //NSLog(@"Value:%@",string);
}
@end
